package com.xianyun.common.core.utils;

import com.xianyun.common.core.domain.DeviceInfo;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

/**
 * 查看当前设备
 * @author mac_zyj
 */
public class DeviceUtils {

    /**
     * 解析 用户代理(User-Agent)
     * @param userAgent 用户代理User-Agent ,UA
     * @return "设备类型:%s,操作系统:%s,浏览器:%s,浏览器版本:%s,浏览器引擎:%s,用户代理(User-Agent):[%s]"
     * @author GongLiHai
     * @date 2020/8/25 11:12
     */
    public static DeviceInfo getDevice(String userAgent) {
        //解析agent字符串
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        //获取浏览器对象
        Browser browser = ua.getBrowser();
        //获取操作系统对象
        OperatingSystem os = ua.getOperatingSystem();
        DeviceInfo deviceInfo=new DeviceInfo();
        if (null != os.getName()){
            deviceInfo.setOsName(os.getName());
        }
        if (null!=browser.getName()){
            deviceInfo.setBrowName(browser.name());
        }
        if (null!= browser.getVersion(userAgent))
        {
            deviceInfo.setVersion(browser.getVersion(userAgent).getVersion());
            deviceInfo.setMajorVersion(browser.getVersion(userAgent).getMajorVersion());
            deviceInfo.setMinorVersion(browser.getVersion(userAgent).getMinorVersion());
        }
       return deviceInfo;
    }
}
